package com.itheima.fanshe2;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/*
获取构造方法对象 并创建对象
 */
public class Demo03 {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<Student> clazz = Student.class;
        //空参构造
        Constructor<Student> constructor = clazz.getConstructor();
        //创建 空白对象
        Student student = constructor.newInstance();

        System.out.println(student);
        //获取 全参构造
        Constructor<Student> constructor1 = clazz.getConstructor(String.class, int.class);
        //创建对象时 传递真实的参数
        Student student2 = constructor1.newInstance("坤坤", 26);
        System.out.println(student2);

        Constructor<Student> privateConstructor = clazz.getDeclaredConstructor(String.class);
        //私有构造方法 不能直接创建对象
        //需要暴力反射
        privateConstructor.setAccessible(true);
        Student student3 = privateConstructor.newInstance("签签");
        System.out.println(student3);


    }
}
